package com.hrm.notice.handler;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hrm.commons.beans.Notice;
import com.hrm.commons.beans.User;
import com.hrm.notice.service.INoticeService;
import com.hrm.utils.PageModel;

@Controller
@RequestMapping("/notice")
public class NoticeHandler {
	
	@Autowired
	private INoticeService noticeService;
	
	@RequestMapping("/findNotice.do")
	public String findNotice(@RequestParam(defaultValue="1")Integer pageIndex,
			Integer user_id, Notice notice, Model model) {
		this.assocation(user_id, notice);
		
		PageModel pageModel = new PageModel();
		pageModel.setPageIndex(pageIndex);
		
		int count = noticeService.findNoticeCount(notice);
		pageModel.setRecordCount(count);
		
		List<User> users = noticeService.findAllUser();
		
		List<Notice> notices = noticeService.findNotice(notice, pageModel);
		
		model.addAttribute("users", users);
		model.addAttribute("notice", notice);
		model.addAttribute("pageModel", pageModel);
		model.addAttribute("notices", notices);
		return "/jsp/notice/notice.jsp";
	}
	
	@RequestMapping("/addNotice.do")
	public String addNotice(String flag, Integer user_id, Notice notice, Model model, HttpSession session) {
		User user = (User) session.getAttribute("login_user");
		notice.setUser(user);
		if(flag.equals("1")) {
			List<User> users = noticeService.findAllUser();
			model.addAttribute("users", users);
			return "/jsp/notice/showAddNotice.jsp";
		}else {
			this.assocation(user_id, notice);
			int rows = noticeService.addNotice(notice);
			if(rows > 0) {
				return "redirect:/notice/findNotice.do";
			}else {
				model.addAttribute("fail", "公告添加失败！");
				return "/jsp/fail.jsp";
			}
		}
	}
	
	@RequestMapping("/modifyNotice.do")
	public String modifyNotice(String flag, Notice notice,
			Integer user_id, Model model, HttpSession session) {
		User user = (User) session.getAttribute("login_user");
		notice.setUser(user);
		if(flag.equals("1")) {
			List<User> users = noticeService.findAllUser();
			
			Notice target = noticeService.findNoticeById(notice.getId());
			model.addAttribute("users", users);
			model.addAttribute("notice", target);
			return "/jsp/notice/showUpdateNotice.jsp";
		}else {
			this.assocation(user_id, notice);
			int rows = noticeService.modifyNotice(notice);
			if(rows > 0){
				return "redirect:/notice/findNotice.do";
			}else{
				model.addAttribute("fail", "公告修改失败！");
				return "/jsp/fail.jsp";
			}
		}
	}
	
	@RequestMapping("/removeNotice.do")
	public String removeNotice(Integer[] ids, Model model){
		int rows = noticeService.removeNotice(ids);
		if(rows > 0){
			return "redirect:/notice/findNotice.do";
		}else{
			model.addAttribute("fail", "公告删除失败！");
			return "/jsp/fail.jsp";
		}
	}
	
	@RequestMapping("/previewNotice.do")
	public String previewNotice(Integer id, Model model) {
		Notice notice = noticeService.findpreviewNoticeById(id);
		model.addAttribute("notice", notice);
		return "/jsp/notice/previewNotice.jsp";
	}
	
	public void assocation(Integer user_id, Notice notice) {
		if(user_id != null) {
			User user = new User();
			user.setId(user_id);
			notice.setUser(user);
		}
	}
	
}